基于MVC三层架构,实现正删改查, using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsRelease.Model;
using System.Data;
namespace NewsRelease.DAL
{
public class AdminDAL
{
#region 定义几个类型
/// <summary>
/// string类型
/// </summary>
static string sql;
/// <summary>
/// 已定义:static Admin a
/// </summary>
static Admin a;
/// <summary>
/// 数据在内存中的缓存,定义一个ds
/// </summary>
static DataSet ds;
/// <summary>
/// 表示DataTable中的一行数据
/// </summary>
static DataRow dr;
#endregion
/// <summary>
/// 插入(用户名和密码)sql语句:,返回是否受影响
/// </summary>
/// <param name="a">定义adminA,然后插入a.UserName和 a.UserPass</param>
/// <returns>返回bool是否受影响</returns>
public static bool Add(Admin a)
{
sql = string.Format("insert into Admin(UserName,UserPass) values ('{0}','{1}')", a.UserName, a.UserPass);
return DBSQLHelper.ExecSqlCommand(sql);
}
/// <summary>
/// 定义admin a执行查询语句
/// </summary>
/// <param name="a">Admin用户名</param>
/// <returns>返回查询结果ture或者flase</returns>
public static bool Only(Admin a)
{
sql = string.Format("select * from admin where UserName='{0}'",a.UserName);
return DBSQLHelper.GetDataset2(sql);
}
/// <summary>
/// 执行修改UserPass,where(条件) UserName=a.UserName
/// </summary>
/// <param name="a">=要修改的【用户】=UserName(条件查询);密码修改为:=UserPass</param>
/// <returns>bool返回是否受影响</returns>
public static bool Update(Admin a)
{
sql=string.Format("update Admin set UserPass='{0}' where UserName='{1}'",a.UserPass,a.UserName);
return DBSQLHelper.ExecSqlCommand(sql);
}
/// <summary>
/// 删除语句
/// </summary>
/// <param name="a">条件为查询name</param>
/// <returns>bool返回是否受影响</returns>
public static bool Delete(Admin a)
{
sql = string.Format("Delete Admin where UserName='{0}'",a.UserName);
return DBSQLHelper.ExecSqlCommand(sql);
}
/// <summary>
/// 通过用户名查询到密码
/// </summary>
/// <param name="name">查询条string类型</param>
/// <returns>用户名和密码</returns>
public static Admin Find(string name)
{
a = new Admin();
string sql = string.Format("select * from admin where UserName='{0}'",name);
dr = DBSQLHelper.GetDateRow(sql);
try
{
a.UserName = name;
a.UserPass = dr["UserPass"].ToString();
return a;
}
catch
{
return null;
}
}
/// <summary>
/// 查询表信息,返回list集合。
/// </summary>
/// <returns>返回的是一个表</returns>
public static IList<Admin> Finds()
{
List<Admin> list = new List<Admin>();
sql = "select * from admin";
ds = DBSQLHelper.GetDataset(sql);
if (ds == null)
{
return null;
}
else
{
foreach (DataRow dr in ds.Tables[0].Rows)//假设dr是ds.Tbale[0].Rows中一个子集
{
a = new Admin();
a.UserName = dr["UserName"].ToString();//方法:object DataRow[string columnName]
a.UserPass = dr["UserPass"].ToString();//获取或设置存储在由名称指定的列中的数据。colummnName:列的名称。
list.Add(a);
}
return list;
}
}
}
}
评论